/*
 * init.c
 *
 *  Created on: 23/dic/2013
 *      Author: massimo
 */

#include <msp430x552x.h>
#include "gyro_init.h"
#include "accel_init.h"
#include "init.h"

/// initialize UART 1
void initUART1(unsigned long bitRate, unsigned long fdco){

	unsigned long baud;

	P4SEL |= BIT4 + BIT5;                       // P4.4,5 alternate function TXD/RXD
	UCA1CTL1 |= UCSWRST;                      // **Put state machine in reset**
	UCA1CTL1 |= UCSSEL_2;                     // SMCLK
	baud = fdco / bitRate;
	if (baud < 255){
		UCA1BR0 = baud;                             // @FDCO MHz: 115200 (see User's Guide)
		UCA1BR1 = 0;
	}
	else{
		UCA1BR1 = baud >> 8;
		UCA1BR0 = baud & 0xFF;
	}
	///UCA1MCTL = UCBRS_0 + UCBRF_13 + UCOS16;   // Modln UCBRSx=0, UCBRFx=0,
												// over sampling
	UCA1CTL1 &= ~UCSWRST;                     	// **Initialize USCI state machine**
	UCA1IE |= UCRXIE;                         // Enable USCI_A0 RX interrupt

}


/// initialize port 1
void initPort1(void){

	P1DIR |= BIT0;
	P1OUT &= ~BIT0;

}

/// ADC init with INTeRnAL REFERENCE
void initADC(unsigned char channel){

	/// set reference register for internal reference: enable, 1.5V
	REFCTL0 = REFMSTR + REFVSEL_0 + REFON;
	/// disable the AD unit
	ADC12CTL0 = 0;
	/// set the AD to have internal ck, not divided, with single channel operation
	ADC12CTL1 =  ADC12SHP;
	/// 12 bit conversion
	ADC12CTL2 = ADC12RES_2;
	if (channel < 12){
		if (channel == 10){
		/// use of internal temperature sensor. It requires 100us sampling time, with a clock of 5MHz:
		/// 100 us * 5MHz = 500, => ADC12SHT0x = 1010b: 512 clock sample time
			ADC12CTL0 |= ADC12SHT0_10 + ADC12ON;
		/// Vref and channel 10 (Temperature sensor)
			ADC12MCTL0 = ADC12SREF_1 + ADC12INCH_10;

		}
		__delay_cycles(300);                      // 35us delay to allow Ref to settle
		                                          // based on default DCO frequency.
		                                          // See Datasheet for typical settle
		                                          // time.
		/// Enable of conversion
		ADC12CTL0 |= ADC12ENC;

	}

}

/// TIMER A1 INIT
void initTIMER(unsigned long fck){

	unsigned int divisore;
	///FDCO		22118400
	/// impostare il timer
	TA1CCTL0 = CCIE;                          		// CCR0 interrupt enabled
	divisore = fck / 640;							/// 22118400 / 640 = 34560 count
													/// Fck = 22118400 / 64 = 345600 => 100 ms
	TA1CCR0 = divisore >> 1;								/// 50 ms @22,1184 MHz
	TA1EX0	= 7;									/// source clock divided by 8
	TA1CTL = TASSEL_2 + ID_3 + MC_1 + TACLR;        // SMCLK, upmode, divide by 8, clear TAR
}


///
/// inizializza il giroscopio
///
void initGyro(gyro *G){
	char stato, valore;
	 /// initialize I2C gyroscope module
	initI2C_B1(FDCO, 800000, GYRO_ADDR);
	initGdata(G);

	 //valore = readI2CByteFromAddress(WHO_AM_I, &stato);
	if (readI2CByteFromAddress(WHO_AM_I, &stato) == 0xD4){
		G->IsPresent = OK;
		valore = readI2CByteFromAddress(CTRL_REG1, &stato);
		/// scrivo nel registro 0x20 il valore 0x0F, cioe' banda minima, modulo on e assi on
		stato =  writeI2CByte(CTRL_REG1, 0x0F);
		if(readI2CByteFromAddress(CTRL_REG1, &stato) == 0x0F)
			G->IsOn = OK;
		/// set FS to 500 degree per sec.
		stato =  writeI2CByte(CTRL_REG4, FS_500);
		valore = readI2CByteFromAddress(CTRL_REG4, &stato);
	}
	else
		G->IsPresent = NOT_PRESENT;
}

///
/// inizializza l'accelerometro.
void initAccel(){
	unsigned char valore, stato;
	/*          INIT ACCELEROMETER REGISTERS                */
	initI2C_B1(FDCO, 800000, ACCEL_ADDR);
	valore = readI2CByteFromAddress(CTRL_REG1_A, &stato);
	stato =  writeI2CByte(CTRL_REG1_A, ODR1 + ODR0 + ZaxEN + YaxEN + XaxEN);
	/*														*/
}
